package sessionBean;

import java.util.List;

import interfaceBeanRemote.sessionClubRemote;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import entityBean.Club;
import entityBean.Clubami;
import entityBean.Investisseur;
import entityBean.Startup;

/**
 * Session Bean implementation class sessionClub
 */
@Stateless
public class sessionClub implements sessionClubRemote {

	@PersistenceContext(unitName = "EJB3_Entity")
	private EntityManager em;

	@PostConstruct
	public void init() {
		System.out.println("calling init method");
	}

	@PreDestroy
	public void cleanup() {
		System.out.println("calling cleanup method");
	}

	/**
	 * Default constructor.
	 */
	public sessionClub() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void addClub(Club club) {
		// TODO Auto-generated method stub
		em.persist(club);
		em.flush();
	}

	@Override
	public Club findClub(int id) {
		// TODO Auto-generated method stub
		return em.find(Club.class, id);
	}
	
	


	@Override
	public Club findClub_byName(String name) {
		Query query = em.createQuery("SELECT c from Club as c where c.nom = '"
				+ name + "'");
		return (Club) query.getSingleResult();
	}
	
	@Override
	public Integer findClubId_byName(String name) {
		Query query = em.createQuery("SELECT c.idclub from Club as c where c.nom = '"
				+ name + "'");
		return (Integer) query.getSingleResult();
	}

	@Override
	public void removeClub(Club club) {
		// TODO Auto-generated method stub
			em.remove(club);

	}
	
	@Override
	public void removeClubById2(int id) {
		em.remove(em.find(Club.class, id));

	}

	@Override
	public int removeClubById(int id) {
		return em.createQuery("DELETE FROM Club c WHERE c.idclub = " + id)
				.executeUpdate();
	}

	@Override
	public void updateClub(Club club) {
		em.merge(club);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Club> getClubListWithMembre() {
		// TODO Auto-generated method stub

		Query query = em
				.createQuery("SELECT DISTINCT c FROM Club AS c left join fetch c.investisseurs"
						+ " ORDER BY c.idclub ASC");
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Club> getClubListWithMembreByID(int id) {
		// TODO Auto-generated method stub

		Query query = em
				.createQuery("SELECT DISTINCT c FROM Club AS c left join fetch c.investisseurs WHERE c.idclub = "
						+ id + " ORDER BY c.idclub ASC");
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Club> getClubListWithClubAmis(int id_club) {
		// TODO Auto-generated method stub
		Query query = em
				.createQuery("SELECT c FROM Club AS c inner join fetch c.clubamis where c.idclub = '"
						+ id_club + "'" + " ORDER BY c.idclub ASC");
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Club> getClubListWithAvancement() {
		// TODO Auto-generated method stub
		Query query = em
				.createQuery("SELECT c FROM Club AS c inner join fetch c.avancements"
						+ " ORDER BY c.idclub ASC");
		return query.getResultList();
	}

	@Override
	public Integer getIdClubOfInvestiseur(int idInv) {
		// TODO Auto-generated method stub
		Query query = em
				.createQuery("SELECT c.idclub FROM Club AS c inner join fetch c.investisseurs "
						+ "WHERE c.investisseurs.idInvestisseur = " + idInv);
		return (Integer) query.getSingleResult();
	}

	@Override
	public void refresh(Club c) {
		// TODO Auto-generated method stub
		em.refresh(c);
	}

	@Override
	public void flush() {
		// TODO Auto-generated method stub
		em.flush();
	}


}
